package com.wellness.commons.http;

import com.wellness.commons.message.BaseMessage;

import java.io.Serializable;

/**
 * API统一返回格式封装类
 *
 * @author: zhuo.shi
 * @since: 2020/3/9
 *
 */
public class APIResponse<T> implements Serializable {

    private String requestId = "";

    private Integer code;
    private String message = "";
    private T data;

    private Long timestamp = 0L;
    private Integer elapsed = 0;

    public APIResponse() {}

    public static <T> APIResponse<T> returnSuccess() {
        APIResponse<T> response = new APIResponse<>();
        response.setCode(BaseMessage.OK_CODE);
        response.setMessage(BaseMessage.OK_MESSAGE);
        return response;
    }

    public static <T> APIResponse<T> returnSuccess(T data) {
        APIResponse<T> response = new APIResponse<>();
        response.setCode(BaseMessage.OK_CODE);
        response.setMessage(BaseMessage.OK_MESSAGE);
        response.setData(data);
        return response;
    }

    public static <T> APIResponse<T> returnFail(int errorCode, String errorMessage) {
        APIResponse<T> response = new APIResponse<>();
        response.setCode(errorCode);
        response.setMessage(errorMessage);
        return response;
    }

    public static <T> APIResponse<T> returnFail(BaseMessage message) {
        APIResponse<T> response = new APIResponse<>();
        response.setCode(message.getCode());
        response.setMessage(message.getMessage());
        return response;
    }

    public String getRequestId() {
        return requestId;
    }

    public void setRequestId(String requestId) {
        this.requestId = requestId;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public Long getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(Long timestamp) {
        this.timestamp = timestamp;
    }

    public Integer getElapsed() {
        return elapsed;
    }

    public void setElapsed(Integer elapsed) {
        this.elapsed = elapsed;
    }
}
